package team.cdlzz.util;

import org.apache.ibatis.annotations.Mapper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import team.cdlzz.dao.UserMapper;
import team.cdlzz.model.pojo.Question;
import team.cdlzz.model.pojo.User;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.*;

/**
 * @author zw
 * @date 2022-06-26.
 */

public class IDUtils {

    static public String ipconfig;

    public static String getPath(String path,Integer type){

        if (type==1){
            if (path==null||path.equals(""))
                return "http://"+ipconfig +":9999/data/new_user.jpg";
            return "http://"+ipconfig +":9999/data/"+path;
        }else if (type==2){
            if (path==null||path.equals("")) {
                return "http://"+ipconfig +":9999/data/012ccaec-d881-40ce-ac55-42a180ac515a1656313261814.png";
            }
            return "http://"+ipconfig +":9999/data/"+path;
        }else if (type==3){
            if (path==null||path.equals("")) {
                return "http://"+ipconfig +":9000/upload/download?filename=";
            }
            return "http://"+ipconfig +":9000/upload/download?filename="+path;
        }else {
            if (path==null||path.equals("")) {
                return "http://"+ipconfig +":9999/data/012ccaec-d881-40ce-ac55-42a180ac515a1656313261814.png";
            }
            return "http://"+ipconfig +":9999/data/"+path;
        }
    }

    static {
        List<String> ipList = new ArrayList<String>();
        InetAddress ip = null;
        try {
            Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                // 遍历全部ip
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = (InetAddress) ips.nextElement();
                    if (null == ip || "".equals(ip)) {
                        continue;
                    }
                    String sIP = ip.getHostAddress();
                    if(sIP == null || sIP.indexOf(":") > -1) {
                        continue;
                    }
                    ipList.add(sIP);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ipconfig = ipList.get(ipList.size()-1);
    }
//    /**
//     * 获取本机ip
//     * @return 本机v4ip
//     */
//    public static   String getLocalIP() {
//
//    }
    /**
     * 唯一ID生成器，可以生成唯一ID
     * @return 唯一ID
     */
    public static String generateUniqueId() {return UUID.randomUUID().toString() + System.currentTimeMillis();}

    public static String generateUserId(String username) {
        return UUID.randomUUID().toString()+username;
    }

    public static Integer setQuestion(String path){
        try {

            Workbook workbook = new HSSFWorkbook(new FileInputStream(new File("D:\\桌面data\\桌面\\2021竞赛公开题库1018.xls")));
            Sheet sheet = workbook.getSheet("Sheet1");
            Row row = sheet.getRow(0);
            int rowNum = row.getPhysicalNumberOfCells(); //当前 0 号列的列数
            short firstCellNum = row.getFirstCellNum();  //获取开始列
            short lastCellNum = row.getLastCellNum();   //获取结束列
            int[] ints = new int[9];
            String[] strings = {"题号","题型","题目","A","B","C","D","答案","难度"};
            for (int i = firstCellNum; i < lastCellNum; i++) {
                Cell cell = row.getCell(i);
                for (int j = 0; j < 8; j++) {
                    System.out.println(cell);
                }
            }
            System.out.println(firstCellNum);
            System.out.println(lastCellNum);
        } catch (IOException e) {
        }

        return 1;
    }

    public static void main(String[] args) {
        setQuestion("");
    }


    public static Double DuplicateChecking(String str1,String str2){
        str1 = str1.replaceAll("(//.*)|\r|\n|\\s", "");
        str2 = str2.replaceAll("(//.*)|\r|\n|\\s", "");
        return Init(str1,str2);
    }


    private static Double Init(String string1,String string2){
        Map<Character, int[]> vectorMap = new HashMap<Character, int[]>();

        int[] tempArray = null;
        for (Character character1 : string1.toCharArray()) {
            if (vectorMap.containsKey(character1)) {
                vectorMap.get(character1)[0]++;
            } else {
                tempArray = new int[2];
                tempArray[0] = 1;
                tempArray[1] = 0;
                vectorMap.put(character1, tempArray);
            }
        }
        for (Character character2 : string2.toCharArray()) {
            if (vectorMap.containsKey(character2)) {
                vectorMap.get(character2)[1]++;
            } else {
                tempArray = new int[2];
                tempArray[0] = 0;
                tempArray[1] = 1;
                vectorMap.put(character2, tempArray);
            }
        }
        return sim(vectorMap);
    }


    // 求余弦相似度
   static private double sim(Map vectorMap) {
        double result = 0;
        result = pointMulti(vectorMap) / sqrtMulti(vectorMap);
        return result;
    }

    private static double sqrtMulti(Map<Character, int[]> paramMap) {
        double result = 0;
        result = squares(paramMap);
        result = Math.sqrt(result);
        return result;
    }

    // 求平方和
    private static double squares(Map<Character, int[]> paramMap) {
        double result1 = 0;
        double result2 = 0;
        Set<Character> keySet = paramMap.keySet();
        for (Character character : keySet) {
            int temp[] = paramMap.get(character);
            result1 += (temp[0] * temp[0]);
            result2 += (temp[1] * temp[1]);
        }
        return result1 * result2;
    }

    // 点乘法
    private static double pointMulti(Map<Character, int[]> paramMap) {
        double result = 0;
        Set<Character> keySet = paramMap.keySet();
        for (Character character : keySet) {
            int temp[] = paramMap.get(character);
            result += (temp[0] * temp[1]);
        }
        return result;
    }

    public static List<Question> getWorkQuestionList(String path) throws IOException {
        List<Question> list = new ArrayList();
        try {
            List<Question> questionslist = new ArrayList<>();
            Workbook workbook = new HSSFWorkbook(new FileInputStream(new File(path)));
            Sheet sheet = workbook.getSheet("Sheet1");
            int rowNum =0;
            Cell cell1,cell2,cell3,cell4,cell5,cell6,cell7,cell8;
            Row row = sheet.getRow(rowNum++);
            short firstCellNum = row.getFirstCellNum();  //获取开始列
            short lastCellNum = row.getLastCellNum();   //获取结束列
            int[] ints = new int[8];
            String[] strings = {"题型","题目","A","B","C","D","答案","难度"};
            int flag= 0;
//            System.out.println(firstCellNum+"  ==  " +lastCellNum);
            for (int i = firstCellNum; i < lastCellNum-1; i++) {
                Cell cell = row.getCell(i);
                for (int j = 0; j < 8&&cell!=null; j++) {
                    if (strings[j].equals(cell.toString())){
                        ints[j] = i;
//                        System.out.println(strings[j]+"==="+cell);
                        flag++;
                        System.out.println(flag);
                        break;
                    }
                }
            }
            int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
            for (;rowNum<physicalNumberOfRows-1;rowNum++){
//                System.out.println(rowNum);
                row = sheet.getRow(rowNum);
                cell1 = row.getCell(ints[0]);
                cell2 = row.getCell(ints[1]);
                cell3 = row.getCell(ints[2]);
                cell4 = row.getCell(ints[3]);
                cell5 = row.getCell(ints[4]);
                cell6 = row.getCell(ints[5]);
                cell7 = row.getCell(ints[6]);
                cell8 = row.getCell(ints[7]);
                Question question = create(cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8);
                list.add(question);
            }

        } catch (IOException e) {
            throw  new IOException("文件格式有问题");
        }
        return list;
    }


    public static Question create(Cell cell1,Cell cell2, Cell cell3,Cell cell4,
                           Cell cell5, Cell cell6,Cell cell7,Cell cell8){
        return new Question(null,cell1!=null?cell1.toString():null,
                cell2!=null?cell2.toString():null,cell3!=null?cell3.toString():null,
                cell4!=null?cell4.toString():null, cell5!=null?cell5.toString():null,
                cell6!=null?cell6.toString():null, cell7!=null?cell7.toString():null,
                cell8!=null?(int)(Double.parseDouble(cell8.toString())):null);
    }

//    public ArrayList<Question> QuestionsByFile(String path){
//
//    }

}
